package com.xiaoshuidi.cloud.module.member.service.point;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.LoginInfo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.MallEncodeResp;
import com.xiaoshuidi.cloud.module.member.util.DigitalSignatureUtil;
import com.xiaoshuidi.cloud.module.member.util.RSAEncryptUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MallEncodeService {

    @Value("${crypto.RSA_PUBLIC_KEY}")
    private String RSA_PUBLIC_KEY;


    @Value("${crypto.SIGNATURE_PRIVATE_KEY}")
    private String SIGNATURE_PRIVATE_KEY;


    /**
     * 加密登录信息
     *
     * @param loginInfo
     * @return
     */
    public MallEncodeResp getEncodeToken(LoginInfo loginInfo) throws Exception {
        MallEncodeResp mallEncodeResp = new MallEncodeResp();
        String content = loginInfo.getUserId() + loginInfo.getAccessToken() + loginInfo.getRefreshToken() + loginInfo.getTenantId();
        String signString = DigitalSignatureUtil.generationSignature(content, Base64.decodeBase64(SIGNATURE_PRIVATE_KEY));

        String cipherConent= JSONObject.toJSONString(loginInfo);
        String ciphertext = RSAEncryptUtil.encrypt(cipherConent, Base64.decodeBase64(RSA_PUBLIC_KEY));
        mallEncodeResp.setSignString(signString);
        mallEncodeResp.setCiphertext(ciphertext);
        return mallEncodeResp;

    }
}
